﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

public class Tools
{
    private static System.Random random = new System.Random();
    public static DateTime UnixTimeStampStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    //获取设备唯一标识
    public static string deviceUniqueIdentifier { get { return SystemInfo.deviceUniqueIdentifier; } }
    /// <summary>
    /// 获取设备型号
    /// </summary>
    public static string deviceModel
    {
        get
        {
#if UNITY_IPHONE && !UNITY_EDITOR
                return Device.generation.ToString();;
#else
            return SystemInfo.deviceModel;
#endif
        }
    }

    public static string deviceName { get { return SystemInfo.deviceName; } }
    /// <summary>
    /// 依据设备标识符生成一个唯一数字ID
    /// </summary>
    /// <param name="fromCode">重fromCode开始</param>
    /// <param name="wei"></param>
    /// <returns></returns>
    public static long GenerateID(int wei)
    {
        string Identifier = deviceUniqueIdentifier;
        byte[] bytes = Encoding.UTF8.GetBytes(Identifier);
        long num = BitConverter.ToInt64(bytes, 0);
        string numStr = num.ToString();
        int sp = numStr.Length - wei;
        double endNum = Convert.ToDouble(numStr.Substring(sp, wei));
        char[] startNumStr = numStr.Substring(0, sp).ToCharArray();
        for (int i = 0; i < startNumStr.Length; i++)
        {
            int index = -1;
            while (true)
            {
                index = Tools.Range(0, startNumStr.Length);
                if (startNumStr[index] != '-') break;
            }
            string subStr = startNumStr[index].ToString();
            startNumStr[index] = '-';
            int itemNum = Convert.ToInt32(subStr);
            string itemStr = itemNum.ToString();
            for (int p = 0; p < itemNum - 1; p++) itemStr += "0";
            itemNum = Convert.ToInt32(itemStr);
            endNum += itemNum;
        }

        return long.Parse(endNum.ToString());
    }

    /// <summary>
    /// 获取当前时间戳，单位秒
    /// </summary>
    /// <returns></returns>
    public static long TimeStampSeconds()
    {
        return (long)(DateTime.Now.ToUniversalTime() - UnixTimeStampStart).TotalSeconds;
    }
    public static long UTCTimeStampSeconds()
    {
        long timeArea = TimeArea() * 3600;
        if (timeArea < 0) timeArea = 0;
        return (TimeStampSeconds() - timeArea);
    }
    /// <summary>
    /// 获取时区
    /// </summary>
    /// <returns></returns>
    public static int TimeArea()
    {
        DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
        return startTime.Hour;
    }
    /// <summary>
    /// 获取时间戳，单位毫秒
    /// </summary>
    /// <returns></returns>
    public static long TimeStamp()
    {
        return (long)(DateTime.Now.ToUniversalTime() - UnixTimeStampStart).TotalMilliseconds;
    }
    public static string DayTime(string flg = "_")
    {
        return DateTime.Now.Year + flg + DateTime.Now.Month + flg + DateTime.Now.Day;
    }

    public static string AddDay(int day, string flg = "_")
    {
        DateTime tomorrow = DateTime.Now.AddDays(day);
        return tomorrow.Year + flg + tomorrow.Month + flg + tomorrow.Day;
    }
    /// <summary>
    /// 获取当前时间
    /// </summary>
    /// <returns></returns>
    public static DateTime DCT()
    {
        return DateTime.Now;
    }

    public static int Range(int min, int max)
    {
        return random.Next(min, max);
    }
    public static double RangeDoble(double min, double max, int wei = 2)
    {
        double result = (double)(random.NextDouble() * (max - min) + min);
        return Convert.ToDouble(result.ToString("f" + wei), CultureInfo.InstalledUICulture);
    }
    /// <summary>
    /// 移除两个字符串直接的全部字符
    /// </summary>
    /// <param name="sourse"></param>
    /// <param name="startstr"></param>
    /// <param name="endstr"></param>
    /// <returns></returns>
    public static string MidStrEx(string sourse, string startstr, string endstr)
    {
        string str = sourse;
        int startindex, endindex;
        while ((startindex = str.IndexOf(startstr))>=0 && (endindex = str.IndexOf(endstr))>=0)
        {
            str = str.Substring(0, startindex) + str.Substring(endindex + endstr.Length);
        }
        return str;
    }
    public static Vector3 ConvertLoaclPoint(RectTransform target, RectTransform parent=null)
    {
        if (parent == null) parent = UIMrg.Instance.GetLayer(0);
        Vector2 endPos;
        endPos = RectTransformUtility.WorldToScreenPoint(UIMrg.Instance.UICamera, target.position);
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, endPos, UIMrg.Instance.UICamera, out endPos);
        return endPos;
    }
    public static Vector3 ConvertlPoint(RectTransform target, RectTransform parent=null)
    {
        if (parent == null) parent = UIMrg.Instance.GetLayer(0);
        Vector3 endPos;
        endPos = RectTransformUtility.WorldToScreenPoint(UIMrg.Instance.UICamera, target.position);
        RectTransformUtility.ScreenPointToWorldPointInRectangle(parent, endPos, UIMrg.Instance.UICamera, out endPos);
        return endPos;
    }

}